package com.bawei.hfgc.touch;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.bawei.hfgc.R;

public class MainActivity extends AppCompatActivity {

    private Button button1;
    private Button button2;
    private ViewGroup myLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
//        // 1.为ViewGroup布局设置监听事件
//        myLayout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.i("TAG", "点击了ViewGroup");
//            }
//        });
//
//        // 2. 为按钮1设置监听事件
//        button1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.i("TAG", "点击了button1");
//            }
//        });
//
//        // 3. 为按钮2设置监听事件
//        button2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.i("TAG", "点击了button2");
//            }
//        });

// 1. 注册Touch事件监听setOnTouchListener 且 在onTouch()返回false
        button1.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                System.out.println("执行了onTouch(), 动作是:" + event.getAction());
//                return false;
                return true;
            }
        });

// 2. 注册点击事件OnClickListener()
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("执行了onClick()");
            }
        });


    }












    private void initView() {
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        myLayout = (LinearLayout) findViewById(R.id.my_layout);
    }
}